package com.promise.jokerdream.ui.settings

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.promise.jokerdream.model.formation.FormationAttrModel
import com.promise.jokerdream.model.formation.attrMap
import com.promise.jokerdream.model.formation.FormationListItemModel
import com.promise.jokerdream.model.formation.FormationTypeModel
import com.promise.jokerdream.model.formation.FormationSelection
import com.promise.jokerdream.ui.components.SettingCard
import com.promise.jokerdream.ui.components.SelectionRow
import com.promise.jokerdream.ui.components.FormationSelectionBottomSheet
import com.promise.jokerdream.ui.theme.AppFontSizes
import com.promise.jokerdream.ui.theme.AppSpacing
import com.promise.jokerdream.viewmodel.FormationViewModel

/**
 * 佣兵助阵设置卡片
 */
@Composable
fun FormationSettingCard(
    modifier: Modifier = Modifier,
    enabled: Boolean,
    onEnabledChange: (Boolean) -> Unit,
    onFormationSelect: (FormationTypeModel, FormationListItemModel, FormationAttrModel) -> Unit,
    selection: FormationSelection = FormationSelection.EMPTY,
    onExecuteClick: (() -> Unit)? = null
) {
    SettingCard(
        title = "佣兵助阵",
        description = "选择提升的助阵",
        checked = enabled,
        onCheckedChange = onEnabledChange,
        modifier = modifier,
        onExecuteClick = onExecuteClick,
        content = {
            if (enabled) {
                FormationSelectionArea(
                    onFormationSelect = onFormationSelect,
                    selection = selection
                )
            }
        }
    )
}

/**
 * 佣兵助阵选择区域
 */
@Composable
private fun FormationSelectionArea(
    onFormationSelect: (FormationTypeModel, FormationListItemModel, FormationAttrModel) -> Unit,
    selection: FormationSelection = FormationSelection.EMPTY
) {
    val viewModel: FormationViewModel = viewModel()
    var showFormationDialog by remember { mutableStateOf(false) }
    
    // 根据保存的配置生成selectedText
    val selectedText = remember(selection) {
        if (selection.isValid && selection.selectedTypeName.isNotEmpty()) {
            "${selection.selectedTypeName} - ${selection.selectedFormationName} - ${selection.selectedAttrName}"
        } else {
            "点击选择助阵"
        }
    }
    
    SelectionRow(
        label = "选择助阵：",
        selectedText = selectedText,
        onClick = { showFormationDialog = true }
    )
    
    if (showFormationDialog) {
        FormationSelectionBottomSheet(
            onDismiss = { showFormationDialog = false },
            onFormationSelect = { type, formation, attr ->
                // 只有在点击完成时才调用回调
                onFormationSelect(type, formation, attr)
                showFormationDialog = false
            },
            selection = selection
        )
    }
}

